package com.bsoft.hospitalchs.ui.child;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bsoft.hospitalchs.R;
import com.bsoft.hospitalchs.base.BaseActivity;
import com.bsoft.hospitalchs.model.SearchResBean;
import com.bsoft.hospitalchs.ui.childcase.ChildCaseFragment;
import com.bsoft.hospitalchs.ui.childinfo.ChildinfoFragment;
import com.bsoft.hospitalchs.ui.childpicture.ChildpictureFragment;
import com.bsoft.hospitalchs.ui.childrecord.ChildrecordFragment;
import com.bsoft.hospitalchs.ui.growth.GrowthFragment;
import com.bsoft.hospitalchs.ui.progress.ProgressFragment;
import com.bsoft.hospitalchs.ui.questionnaire.QuestionnaireFragment;
import com.bsoft.hospitalchs.ui.reservation.ReservationFragment;
import com.bsoft.hospitalchs.widget.CustomProgress;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 泅渡者
 * Created on 2017/5/18.
 */

public class ChildInfoActivity extends BaseActivity<ChildInfoContract.Presenter> implements ChildInfoContract.View {

    @Bind(R.id.ll_back)
    LinearLayout llBack;
    @Bind(R.id.tv_name)
    TextView tv_name;
    @Bind(R.id.tv_sex)
    TextView tv_sex;
    @Bind(R.id.tv_year)
    TextView tv_year;
    @Bind(R.id.tv_date)
    TextView tv_date;
    @Bind(R.id.lv_left)
    ListView lvLeft;
    private ChildrecordFragment childrecordFragment;
    private ChildinfoFragment childinfoFragment;
    private ChildpictureFragment childpictureFragment;
    private ChildCaseFragment childCaseFragment;
    private ReservationFragment reservationFragment;
    private ProgressFragment progressFragment;
    private FragmentTransaction transaction;
    private GrowthFragment growthFragment;
    private QuestionnaireFragment questionnaireFragment;
    private String curFragmentTag;
    private SearchResBean searchResBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child_info_new);
        ButterKnife.bind(this);
        this.createPresenter(new ChildInfoPresenter(this, this));
        childinfoFragment = new ChildinfoFragment(); //个人信息
        childrecordFragment = new ChildrecordFragment();//预检登记
        childpictureFragment = new ChildpictureFragment();//照片记录
        childCaseFragment = new ChildCaseFragment();
        reservationFragment = new ReservationFragment();
        progressFragment = new ProgressFragment();
        growthFragment = new GrowthFragment();//生长曲线
        questionnaireFragment = new QuestionnaireFragment();
        presenter.setLeftMenu(this, lvLeft, R.id.fm_layout);
        Gson gson = new Gson();
        searchResBean = gson.fromJson(getIntent().getStringExtra("SearchResBean").toString(), new TypeToken<SearchResBean>() {
        }.getType());

        tv_name.setText(searchResBean.getBody().getPERSONNAME());
        tv_date.setText(searchResBean.getBody().getHEALTHCAREDATE());
        tv_sex.setText(searchResBean.getBody().getSEXNAME());
        tv_year.setText(searchResBean.getBody().getAGE());
    }

    @Override
    public void hideLoading() {
        if (customProgress != null) {
            customProgress.dismiss();
        }
    }

    @Override
    public void showLoadingDialog() {
        customProgress = CustomProgress.show(this, "正在加载...", true, null);
    }

    @OnClick(R.id.ll_back)
    public void onViewClicked() {
        this.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment f = getSupportFragmentManager().findFragmentByTag(curFragmentTag);
        f.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void setFragment(int position) {

        transaction = getSupportFragmentManager().beginTransaction();
        switch (position) {
            case 0:
                curFragmentTag = "childinfoFragment";
                transaction.replace(R.id.fm_layout, childinfoFragment, "childinfoFragment");
                transaction.commit();
                break;
            case 1:
                curFragmentTag = "childrecordFragment";
                transaction.replace(R.id.fm_layout, childrecordFragment, "childrecordFragment");
                transaction.commit();
                break;
            case 2:
                curFragmentTag = "childpictureFragment";
                transaction.replace(R.id.fm_layout, childpictureFragment, "childpictureFragment");
                transaction.commit();
                break;
            case 3:
                curFragmentTag = "progressFragment";
                transaction.replace(R.id.fm_layout, progressFragment, "progressFragment");
                transaction.commit();
                break;
            case 4:
                curFragmentTag = "childCaseFragment";
                transaction.replace(R.id.fm_layout, childCaseFragment, "childCaseFragment");
                transaction.commit();
                break;
            case 6:
                curFragmentTag = "reservationFragment";
                transaction.replace(R.id.fm_layout, reservationFragment, "reservationFragment");
                transaction.commit();
                break;
            case 7:
                curFragmentTag = "growthFragment";
                transaction.replace(R.id.fm_layout, growthFragment, "growthFragment");
                transaction.commit();
                break;
            case 5:
                curFragmentTag = "questionnaireFragment";
                transaction.replace(R.id.fm_layout, questionnaireFragment, "questionnaireFragment");
                transaction.commit();
                break;
            default:
                break;
        }
    }
}
